import { message } from 'ant-design-vue';

export function useDownload() {
  function handleRes(res: any) {
    const url = window.URL.createObjectURL(new Blob([res.data]));
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', 'data.xlsx');
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }
  async function downloadAll() {
    try {
      const res: any = await exportAllData();
      handleRes(res);
    }
    catch (error) {
      message.error('下载失败');
    }
  }
  async function downloadById(selectedId: number[]) {
    try {
      const res: any = await exportData(selectedId);
      handleRes(res);
    }
    catch (error) {
      message.error('下载失败');
    }
  }
  async function downExcel() {
    await getDownExcel().then((res: any) => {
      handleRes(res);
    });
  }
  return {
    handleRes,
    downloadAll,
    downloadById,
    downExcel,
  };
}
